<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Plugin Controller
 *
 * @package		SimplyPost
 * @subpackage	Admin
 * @category	Controller
 * @author		Pascal Kriete
 */

class Plugins extends Administrator {

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Plugins()
	{
		parent::Administrator();
		$this->lang->load('admin');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Shows a list of installed plugins
	 *
	 * @access	public
	 */
	function index()
	{
		set_title($this->lang->line('plugin_mgmt_title'));
		$this->breadcrumb->from_path();
		
		$this->load->helper('form');
		
		$query = $this->db->get('plugins');
		$tmp = $query->result_array();
		
		$names = array();
		
		foreach($tmp as $class)
		{
			if (! file_exists(APPPATH.'plugins/'.$class['name'].'.php'))
			{
				$this->db->delete('plugins', array('name' => $class['name']));
			}
			else
			{
				$class['config'] = FALSE;
				
				if ($handle = $this->plugin->get_plugin($class['name']))
					if(method_exists($handle, 'configure'))
						$class['config'] = TRUE;
				
				$names[] = $class;
			}
		}
		
		/* Used to use: <?= form_checkbox('{name}', 'enabled', '{enabled}') ?> */
		
		$data['plugins'] = $names;
		
		$this->parser->parse('admin/plugins', $data);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Configure a specific plugin
	 *
	 * @access	public
	 * @param	string	name of plugin
	 */
	function configure($plugin)
	{
		$handle = $this->plugin->get_plugin($plugin);
		if (! $handle)
		{
			$this->session->set_flashdata('msg', $this->lang->line('plugin_not_exist'));
			redirect('admin/plugins');
		}
		else
		{
			set_title($this->lang->line('plugin_config_title').ucfirst($plugin));
			$this->breadcrumb->from_path();
			
			ob_start();
			
			$this->plugin->configure($handle);
			
			$output =& load_class('Output');
			$output->append_output(ob_get_contents());
			@ob_end_clean();
		}
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Remove a specific plugin
	 *
	 * @access	public
	 * @param	string	name of plugin
	 */
	function remove($plugin)
	{
		$handle = $this->plugin->get_plugin($plugin);
		
		$path = APPPATH.'plugins/'.$plugin.'.php';
		
		if (!$handle)
		{
			if (!file_exists($path))
			{
				// Wtf? Where did it go?
				$this->session->set_flashdata('msg', $this->lang->line('plugin_not_found'));
				redirect('admin/plugins');
			}
			
			include($path);
			$handle =& new $plugin();			
		}
		
		$this->plugin->remove($handle);
		
		unlink($path);
		
		$this->session->set_flashdata('msg', $this->lang->line('plugin_deleted'));
		redirect('admin/plugins');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Enables/disables plugins based on checkbox status
	 *
	 * @access	public
	 */
	function change_status()
	{
		$query = $this->db->get('plugins');
		$tmp = $query->result_array();
		
		foreach($tmp as $plugin)
		{
			$this->db->where('name', $plugin['name']);
			
			if (! $this->input->post($plugin['name']))
			{
				$this->db->update('plugins', array('enabled' => 0));
			}
			else
			{
				$this->db->update('plugins', array('enabled' => 1));
			}
		}
		$this->session->set_flashdata('msg', $this->lang->line('plugin_updated'));
		redirect('admin/plugins');
	}

}